package com.tianlan.blog.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tianlan.blog.dto.UpdateRoleResource;
import com.tianlan.blog.entity.SysRole;
import com.tianlan.blog.entity.SysRoleResource;
import com.tianlan.blog.service.SysRoleResourceService;
import com.tianlan.blog.service.SysRoleService;
import com.tianlan.common.annotation.log.AroundLog;
import com.tianlan.common.base.PageResult;
import com.tianlan.common.base.Result;
import com.tianlan.common.page.MyPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: RoleController
 * @Description: Role前端控制器
 * @author tl-wxq
 * @date 2018年5月18日 下午4:06:17
 */

@RestController
@RequestMapping("/role")
@Api(value = "RoleController|角色相关的前端控制器")
public class RoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysRoleResourceService sysRoleResourceService;
	
    /**
     * 查询角色列表
     */
    @GetMapping("/list")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "查询角色列表")
    public PageResult<SysRole> list(SysRole role, MyPage<SysRole> page) {
    	List<SysRole> roleList = sysRoleService.selectSysRoleList(role,page);
    	page.setRecords(roleList);
    	page.setTotal(sysRoleService.selectSysRoleCount(role));
        PageResult<SysRole> pageResult = new PageResult<SysRole>(page.getTotal(), page.getRecords());
        return pageResult;
    }
    
    /**
     * 添加角色
     */
    @PostMapping("/create")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "添加角色")
    @AroundLog(name = "添加角色")
    public Result<?> create(@RequestBody SysRole role) {
        boolean result = sysRoleService.createRole(role);
        if (result) {
            return new Result<>().success("添加成功");
        } else {
            return new Result<>().error("添加失败，请重试");
        }
    }
    
    /**
     * 修改角色
     */
    @PostMapping("/update")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "更新角色")
    @AroundLog(name = "更新角色")
    public Result<?> update(@RequestBody SysRole role) {
        boolean result = sysRoleService.updateRole(role);
        if (result) {
            return new Result<>().success("修改成功");
        } else {
            return new Result<>().error("修改失败");
        }
    }
    
    /**
     * 删除角色
     */
    @RequiresRoles("SYSADMIN")
    @PostMapping("/delete/{roleId}")
    @ApiOperation(value = "删除角色")
    @AroundLog(name = "删除角色")
    @ApiImplicitParam(paramType = "path", name = "roleId", value = "角色ID", required = true, dataType = "Integer")
    public Result<?> delete(@PathVariable("roleId") Integer roleId) {
        if (null == roleId) {
            return new Result<>().error("ID不能为空");
        }
        boolean result = sysRoleService.deleteRole(roleId);	
        if (result) {
            return new Result<>().success("删除成功");
        } else {
            return new Result<>().error("删除失败");
        }
    }
    
    /**
     * 修改角色状态
     */
    @PostMapping("/status/{roleId}/{roleStatus}")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "修改角色状态")
    @AroundLog(name = "修改角色状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "roleStatus", value = "角色状态", required = true, dataType = "String", paramType = "path") })
    public Result<?> updateStatus(@PathVariable("roleId") Integer roleId,
            @PathVariable("roleStatus") String roleStatus) {
        if (null == roleId || StringUtils.isEmpty(roleStatus)) {
            return new Result<>().error("ID和状态不能为空");
        }
        boolean result = sysRoleService.updateRoleById(roleId,roleStatus);
        if (result) {
            return new Result<>().success("修改成功");
        } else {
            return new Result<>().error("修改失败");
        }
    }
    
    /**
     * 获取角色资源
     * @param roleId
     * @return
     */
    @GetMapping(value = "/resource/{roleId}")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "获取角色的权限资源")
    @ApiImplicitParam(paramType = "path", name = "roleId", value = "角色ID", required = true, dataType = "Integer")
    public Result<List<SysRoleResource>> queryRoleResource(@PathVariable("roleId") Integer roleId) {
        List<SysRoleResource> list = sysRoleResourceService.selectSysRoleResourceByRoleId(roleId);
        return new Result<List<SysRoleResource>>().success().put(list);
    }
    
    /**
     * 修改角色资源
     * @param updateRoleResource
     * @return
     */
    @PostMapping(value = "/resource/update")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "修改角色的权限资源")
    @AroundLog(name = "修改角色的权限资源")
    public Result<?> updateRoleResource(@RequestBody UpdateRoleResource updateRoleResource) {
        boolean result = sysRoleResourceService.updateList(updateRoleResource);
        if (result) {
            return new Result<>().success("修改成功");
        } else {
            return new Result<>().error("修改失败");
        }
    }

    
	/**
     * 查询所有角色列表
     * @return
     */
    @GetMapping(value = "/all")
    @RequiresAuthentication
    @ApiOperation(value = "查询所有角色列表")
    public Result<List<SysRole>> queryAll() {        
        List<SysRole> result = sysRoleService.getSysRole();
        return new Result<List<SysRole>>().success().put(result);
    }
    
    @PostMapping(value = "/name/check")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "校验角色名称是否存在", notes = "校验角色名称是否存在")
    public Result<Boolean> checkRoleName(SysRole role) {
        long count = sysRoleService.checkRoleName(role);
        if (count == 0){
            return new Result<Boolean>().success().put(true);
        } else{
            return new Result<Boolean>().success().put(false);
        }
    }

    @PostMapping(value = "/key/check")
    @RequiresRoles("SYSADMIN")
    @ApiOperation(value = "校验角色标识是否存在", notes = "校验角色标识是否存在")
    public Result<Boolean> checkRoleKey(SysRole role) {
    	long count = sysRoleService.checkRoleKey(role);
        if (count == 0){
            return new Result<Boolean>().success().put(true);
        } else{
            return new Result<Boolean>().success().put(false);
        }
    }
	
}
